package com.lankystar.todolist.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lankystar.common.Page;
import com.lankystar.todolist.dao.TaskMapper;
import com.lankystar.todolist.dao.TaskallocationMapper;
import com.lankystar.todolist.dao.TaskrecordMapper;
import com.lankystar.todolist.model.Task;
import com.lankystar.todolist.model.Taskrecord;
import com.lankystar.todolist.service.ITaskService;

@Component
public class ITaskServiceImpl implements ITaskService {
	@Autowired
	private TaskMapper taskDao;
	@Autowired
	private TaskrecordMapper taskrecordDao;
	@Autowired
	private TaskallocationMapper taskallocationDao;
	
	@Override
	public Page<Map<String, String>> getTasks(Page<Map<String, String>> page) {
		List<Map<String, String>> list = taskDao.findAllByPage(page);
		page.setDatas(list);
		return page;
	}

	@Override
	public boolean insert(Task record) {
		// TODO Auto-generated method stub
		int flag = taskDao.insert(record);
		if (flag == 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 未分配任务
	 */
	@Override
	public List<Map<String, String>> getAssignTasks() {
		// TODO Auto-generated method stub
		List<Map<String, String>> list = taskDao.findAllByAssignPage();
		return list;
	}
	
	/**
     * 关联查询任务记录表
     * @return
     */
	@Override
	public List<Map<String, String>> getTaskrecord() {
		return this.taskrecordDao.selectTaskrecord();
	}
	
	/**
	 *根据主键id查找 
	 */
	@Override
	public Taskrecord selectTaskrecordById(Integer trId) {
		Taskrecord record =taskrecordDao.selectTaskrecordById(trId);
		System.out.println(record);
		return record;
	}
	
	/**
	 * 更新进度
	 */
	@Override
	public void updateTaskrecord(Taskrecord record) {

		taskrecordDao.updateByPrimaryKeySelective(record);
	}
	
	@Override
	public List<Map<String, String>> getTeam() {
		return taskDao.findTeam();
	}

	/**
	 * 插入分配任务
	 */
	@Override
	public boolean insert(Taskrecord record) {
		// TODO Auto-generated method stub
		int flag = taskallocationDao.inserTask(record.getTaskid(), record.getUserid(), record.getCreatetime());
		taskrecordDao.inserTaskrecord(record.getTaskid(), record.getUserid(), record.getCreatetime());
		if (flag == 0) {
			return false;
		} else {
			return true;
		}

	}

	/**
	 * 查看任务
	 */
	@Override
	public List<Map<String, String>> lookTask() {
		// TODO Auto-generated method stub
		List<Map<String,String>> list=taskallocationDao.showTask();
		return list;
	}
}
